1. Field of the Invention
The present invention generally relates to computer processing techniques and, in particular, to a system and method for enabling selective execution of certain code within a computer program without significantly affecting the execution performance of the program.
2. Related Art
In some computer applications, it is desirable to perform certain functionality only in a limited number of circumstances. For example, a computer program may include testing code that tests for certain conditions to ensure that the program is running correctly. As an example, the testing code may include one or more assertions, which are well known statements for testing computer programs. In this regard, assertions are inserted into a program being compiled, and when executed, an assertion tests for a certain condition. When the tested condition is true, the foregoing program is usually terminated, and an error message is displayed.
While it may be desirable to run this testing code during the debugging and/or testing phases, it may be less desirable to run this testing code in normal operation, since the testing code utilizes processor time. In this regard, execution of the testing code is not required for the computer program to operate correctly and is only useful if there is an error that can be detected by the testing code. Thus, executing the testing code when the computer program runs free of errors needlessly utilizes processor time.
As a result, most computer programs that include testing code are usually recompiled once the testing and debugging phases are complete. During recompilation, insertion of testing code into the recompiled programs is prevented such that the recompiled programs do not include the testing code. The recompiled programs run more efficiently since there is no testing code to execute, and it is these recompiled programs that are usually sold to and used by consumers.
However, even after the testing and debugging phases have been completed, there may still be some bugs in the computer program that could be detected by the removed testing code, and once the testing code has been removed, the computer program lacks the capability of detecting these bugs. Therefore, a tradeoff exists between leaving testing code in a computer program and removing the testing code from the computer program. In this regard, the testing code may be removed from the computer program to improve the performance of the program, or the testing code may be left in the program to test for certain errors, thereby adversely affecting the program""s performance.
Thus, a heretofore unaddressed need exists in the industry for providing a system and method of enabling selective execution of certain code (e.g., testing code) within a computer program based on inputs at run time with minimal or no affect to the program""s performance when execution of the foregoing code is not desired.
The present invention overcomes the inadequacies and deficiencies of the prior art as discussed hereinbefore. Generally, the present invention provides a system and method for selectively executing sets of code in computer programs.
The system of the present invention utilizes memory for storing a computer program and processing circuitry for executing instructions of the computer program. In particular, the computer program includes at least one branch instruction and a set of code that is to be selectively enabled or disabled. The branch instruction includes an address identifier identifying a memory address to which the processing circuitry may branch when executing the branch instruction. The processing circuitry, in executing the computer program, receives run time data indicative of whether the set of code is enabled or disabled, and based on the run time data, the processing circuitry sets a value of a mode indicator. While the program is running, the processing circuitry executes the branch instruction. In executing the branch instruction, the processing circuitry, depending on the value of the mode indicator, branches to the address identified by address identifier or branches to a different address. The set of code is located at one of the foregoing addresses. Thus, execution of the set of code is either enabled or disabled by controlling the value of the mode indicator.
The present invention can also be viewed as providing a method for selectively executing sets of code in computer programs. The method can be broadly conceptualized by the following steps: storing a computer program in memory, the computer program having a set of code and a branch instruction, the branch instruction including an address identifier identifying a first address in the memory; receiving, during a run of the program, run time data indicating whether the set of code is enabled; setting a value of a mode indicator based on the run time data; identifying a second address in the memory in response to the branch instruction; branching to the second address based on said identifying step and said value of said mode indicator; and executing an instruction at the second address in response to the branching step.